import { FinanceWithdrawService } from 'services/Api'

/* 提现状态审核相关
-------------------------- */
export const listApplyMixin = {
  data () {
    return {
      apply: '', // 能否提现:0为允许,1为不允许,''不改变
      remark: '',
      applyIndex: '',
      currentApply: '' // 当前状态操作
    }
  },
  computed: {
    // 修改审核参数
    applyParam () {
      return {
        acc_id: this.acc_id,
        ids: this.selectIds,
        status: this.apply,
        reason: this.remark
      }
    }
  },
  methods: {
    // 用户选择完毕
    async applyChangeOk () {
      // 如果选择拒绝，还需要填写拒绝原因
      if (this.apply === '3') {
        const validator = (v) => {
          return !!v
        }
        try {
          const res = await this.PromptTemp('驳回时需要提交拒绝原因', validator, '请输入驳回原因')
          this.confirmApply(res.value)
        } catch (e) {
          this.apply = ''
          this.remark = ''
        }
      } else {
        this.confirmApply()
      }
    },
    // 确认提交
    confirmApply (v) {
      if (v) this.remark = v
      typeof this.applyIndex === 'number'
        ? this.AsyncLoading(this.currentApply, this.applyIndex, this.applyApi)
        : this.AsyncLoading(this.currentApply, this.applyApi)
    },
    // 审核单个
    applyOne (id, index, status) {
      this.selectIds = id
      this.applyIndex = index
      this.currentApply = this._applyRule(status)
      this.ConfirmTemp(`确认${this.currentApply}吗`, () => {
        this.applyChangeOk()
      })
    },
    // 批量审核已选的数据(idKey: 行数据中id对应的字段名)
    applySelected (idKey, status) {
      if (!this.selectList.length) return this.$message.warning('选择数据后才能操作')
      this.selectIds = this.selectList.map(item => item[idKey]).join(',')
      this.applyIndex = ''
      this.currentApply = this._applyRule(status)
      this.ConfirmTemp(`确认${this.currentApply}吗`, () => {
        this.applyChangeOk()
      })
    },
    // 改变审核接口
    currentApplyApi () {
      return FinanceWithdrawService.alterUserWithdrawStatus(this.applyParam)
    },
    // 接口
    async applyApi () {
      const [ err ] = await this.currentApplyApi()
      this.apply = ''
      this.remark = ''
      if (err) return this.$message.error('操作失败')
      this.$message.success(`操作成功`)
      this.getTableData()
    },
    // 状态规则(后台默认：0：申请已经提交，1：申请已经受理，2：转账已经完成，3：被驳回)
    _applyRule (status) {
      this.apply = status
      return status === '2' ? '已打款' : '驳回'
    }
  }
}
